package com.tedu.jsd2407_ivos_fzj.application.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.tedu.jsd2407_ivos_fzj.application.mapper.ApplicationMapper;
import com.tedu.jsd2407_ivos_fzj.application.pojo.dto.ApplicationQuery;
import com.tedu.jsd2407_ivos_fzj.application.pojo.dto.ApplicationSaveParam;
import com.tedu.jsd2407_ivos_fzj.application.pojo.entity.Application;
import com.tedu.jsd2407_ivos_fzj.application.pojo.vo.ApplicationVO;
import com.tedu.jsd2407_ivos_fzj.application.service.ApplicationService;
import com.tedu.jsd2407_ivos_fzj.audit.mapper.AuditMapper;
import com.tedu.jsd2407_ivos_fzj.audit.pojo.vo.AuditVO;
import com.tedu.jsd2407_ivos_fzj.audit.service.AuditService;
import com.tedu.jsd2407_ivos_fzj.base.enums.ApplicationStatusEnum;
import com.tedu.jsd2407_ivos_fzj.user.mapper.UserMapper;
import com.tedu.jsd2407_ivos_fzj.vehicle.mapper.VehicleMapper;
import com.tedu.jsd2407_ivos_fzj.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Service
@Slf4j
@Transactional//保证事务的原子性
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    private ApplicationMapper applicationMapper;
    @Autowired
    private AuditService auditService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AuditMapper auditMapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public void save(ApplicationSaveParam applicationSaveParam) {
        log.debug("保存申请单业务，参数{}",applicationSaveParam);
        Application application = new Application();
        BeanUtils.copyProperties(applicationSaveParam,application);
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        application.setCreateTime(new Date());
        log.debug("插入前的申请单数据，{}",application);
        applicationMapper.insert(application);


        /*还需要为此申请单生成对应的审批单数据*/
        //此时无申请单id
        //可以在sql中添加 useGeneratedKeys="true" keyProperty="id"
            //作用：JDBC为application对象生成自增主键，并将生成的主键值赋值给application对象的id属性
        log.debug("插入后的申请单数据，{}",application);
        auditService.insertAudit(application);
        

    }

    @Override
    public List<ApplicationVO> select(ApplicationQuery applicationQuery) {
        log.debug("申请单查询业务，参数：{}",applicationQuery);
        List<ApplicationVO> applicationVOList = applicationMapper.select(applicationQuery);

        /*遍历所有的申请单，补充对应的审批人*/
        for(int i = 0 ;i<applicationVOList.size();i++){
            ApplicationVO applicationVO = applicationVOList.get(i);
            assignAuditUserList (applicationVO);
        }
        return applicationVOList;
    }

    @Override
    public void cancel(Long id) {
        Application application = new Application();
        application.setId(id);
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        applicationMapper.update(application);
        //删除审批单
        auditMapper.deleteByApplicationId(id);
    }

    @Override
    public void distribute(Long applicationId, Long vehicleId) {
        log.debug("分配车辆业务，参数1：{}，参数2：{}",applicationId,vehicleId);
        Application application = new Application();
        application.setId(applicationId);
        application.setVehicleId(vehicleId);
        application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("2");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }

    @Override
    public void repaid(Long applicationId, Long vehicleId) {
        log.debug("还车业务，参数1：{}，参数2：{}",applicationId,vehicleId);
        Application application = new Application();
        application.setId(applicationId);
        application.setStatus(ApplicationStatusEnum.END.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("1");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }


    public void assignAuditUserList(ApplicationVO applicationVO){
        List<Long> auditUserIdList = new ArrayList<>();
        List<String> auditUsernameList = new ArrayList<>();
        List<AuditVO> auditVOList = auditService.selectByApplicationId(applicationVO.getId());
        for(int i = 0;i<auditVOList.size();i++){
            Long auditUserId = auditVOList.get(i).getAuditUserId();
            auditUserIdList.add(auditUserId);
            String auditUsername = userMapper.selectAudit(auditUserId).getUsername();
            auditUsernameList.add(auditUsername);
        }
        applicationVO.setAuditUserIdList(auditUserIdList);
        StringJoiner stringJoiner = new StringJoiner(",");//拼接工具，以“,”连接
        for(String username : auditUsernameList){
            stringJoiner.add(username);
        }
        applicationVO.setAuditUsernameStr(stringJoiner.toString());

    }
}
